#include <udk.h>
#include "platform.h"
#include "pullcuber.h"
#include "standalonecuber.h"
#include "cuberproxy.h"
#include "protocmd.h"

UDK_HANDLE UDK_API 
	UdkConnect(int* prot,const char *parameters){
  if(0==*prot){
	//detect
	//UDK_LOGD("auto detect with prot[%d],parameters[%s]\n",*prot,parameters);
	CCuberProxy* cp = new CCuberProxy(new CPullCuber());
	CCuberProxy* cpcn = cp->Connect(parameters);
    int lasterror = cp->GetLastError();
	if(cpcn) {
		*prot = UDK_PROT_PULL;
		return (UDK_HANDLE)cpcn;
	} else if (lasterror == ERR_CMD_COMKEY_ERROR || lasterror == ERR_GETTABLE_STRUCT) {
        return NULL;
    }
	delete cp;
	cp = new CCuberProxy(new CStandaloneCuber());
	cpcn = cp->Connect(parameters);
	if(cpcn){
		*prot = UDK_PROT_STANDALONE;
		return (UDK_HANDLE)cpcn;
	}
	delete cp;
  }else if(UDK_PROT_PULL == *prot){
	CCuberProxy* cp = new CCuberProxy(new CPullCuber());
	CCuberProxy* cpcn = cp->Connect(parameters);
	if(cpcn) {	
		return (UDK_HANDLE)cpcn;
	}	
	delete cp;
  }else if(UDK_PROT_STANDALONE == *prot){
	CCuberProxy* cp = new CCuberProxy(new CStandaloneCuber());
	CCuberProxy* cpcn = cp->Connect(parameters);
	if(cpcn){
		*prot = UDK_PROT_STANDALONE;
		return (UDK_HANDLE)cpcn;
	}
	delete cp;	
  }
  return NULL;
}

void UDK_API 
	UdkDisconnect(UDK_HANDLE h){
	CCuberProxy* cp = (CCuberProxy*)h;
	cp->Disconnect();
	delete cp;
}
int UDK_API
	UdkGetLastError(UDK_HANDLE h){
    int lasterror=0;
    if(h){
      CCuberProxy* cp = (CCuberProxy*)h;
      return cp->GetLastError();
    }
    CCuberProxy* cp = new CCuberProxy(new CPullCuber());
    lasterror = cp->GetLastError();
    delete cp;
    UDK_LOGD("lasterror from pull: %d\n", lasterror);
    if(!lasterror || !(//lasterror == ERR_NORESPOSE ||
                       lasterror == ERR_CMD_UNAUTH ||
                       lasterror == ERR_CMD_COMKEY_ERROR ||
                       lasterror == ERR_CMD_INVALID_SESSION_ERROR ||
                       lasterror == ERR_NOTABLE_STRUCT ||
                       lasterror == ERR_NOFIELD_INSTRUCT ||
                       lasterror == ERR_GETTABLE_STRUCT)){
      CCuberProxy* cp = new CCuberProxy(new CStandaloneCuber());
      lasterror = cp->GetLastError();;
      delete cp;
    }
    return lasterror;
}

int UDK_API
	UdkSetLockState(UDK_HANDLE h,int lock_no,int opentime){
	CCuberProxy* cp = (CCuberProxy*)h;
	return cp->SetLockState(lock_no,opentime);
}

int UDK_API
	UdkGetLockState(UDK_HANDLE h,int lock_no){
	CCuberProxy* cp = (CCuberProxy*)h;
	return cp->GetLockState(lock_no);
}

int UDK_API
	UdkGetLockCount(UDK_HANDLE h){
	CCuberProxy* cp = (CCuberProxy*)h;
	return cp->GetLockCount();
}

int UDK_API
UdkSearchDevices(char *CommType,char *Address, char *Buffer,int *BufferLength){
	int totalcount=0,totalbuflen,buflen;
	int ret;
	totalbuflen=buflen=*BufferLength;
	*BufferLength = 0;
	CCuberProxy* cp = new CCuberProxy(new CPullCuber());
	ret = cp->SearchDevices(CommType,Address,Buffer,&buflen);
	delete cp;
	if(ret>0) {
	  totalcount=ret;
	  *BufferLength = buflen;	  
	}
       
    buflen = totalbuflen-*BufferLength;
	cp = new CCuberProxy(new CStandaloneCuber());
	ret = cp->SearchDevices(CommType,Address,Buffer+*BufferLength,&buflen);
	delete cp;
	if(ret>0) {
      totalcount+=ret;
	  *BufferLength+=buflen;
	}
    return totalcount;
}

int UDK_API
	UdkGetComPwd(UDK_HANDLE h, char *buffer, int *BufferLen)
{
	CCuberProxy* cp = (CCuberProxy*)h;

	return cp->GetComPwd(buffer, BufferLen);
}

int UDK_API
	UdkMobileAtt(UDK_HANDLE h, int operate, char *premeters, char *Buffer,int* BufferLength)
{
	CCuberProxy* cp = (CCuberProxy*)h;

	return cp->MobileAtt(operate, premeters, Buffer, BufferLength);
}

int UDK_API
	UdkGetDeviceParam(UDK_HANDLE h, char *Buffer, int Bufferlen, char *Item)
{
	CCuberProxy* cp = (CCuberProxy*)h;

	return cp->GetDeviceParam(Buffer, Bufferlen, Item);
}
 
int UDK_API
	UdkSetDeviceParam(UDK_HANDLE h, char *ItemandValues)
{
	CCuberProxy* cp = (CCuberProxy*)h;
	return cp->SetDeviceParam(ItemandValues);
}

int UDK_API
	UdkSetDownloadProcessCallBack(UDK_HANDLE h, void *pCallBackFun)
{
	CCuberProxy* cp = (CCuberProxy*)h;
	return cp->SetDownloadProcessCallBack(pCallBackFun);
}

int UDK_API
	UdkResetDeviceExt(UDK_HANDLE h, char *parameters)
{
	int ret = 0;
	CCuberProxy* cp = (CCuberProxy*)h;
	ret = cp->ResetDeviceExt(parameters);
	if(1 == ret)
	{
		delete cp;
	}
	return ret;
}